package org.mzyyn.qr.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.mzyyn.qr.entity.AuctionGoodsPO;
import org.mzyyn.qr.entity.AuctionOrderPO;
import org.mzyyn.qr.enums.GoodStatusEnum;
import org.mzyyn.qr.service.auction.AuctionGoodsService;
import org.mzyyn.qr.service.auction.AuctionOrderService;
import org.mzyyn.qr.service.auction.AuctionService;
import org.mzyyn.qr.vo.auction.BidRedisVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author lipeng
 * @description 结束任务
 * @date 2024/9/27 16:39
 */
@Component
@Slf4j
public class AuctionEndTask {

    @Autowired
    AuctionGoodsService auctionGoodsService;

    @Autowired
    AuctionOrderService auctionOrderService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    AuctionService auctionService;

    @Scheduled(initialDelay = 1000, fixedRate = 1000 * 5)
    public void endTask(){
        try {
            log.info("开始进行拍卖结束任务....");
            //获取任务
            List<AuctionGoodsPO> goodsList = getGoodsList();
            if (CollUtil.isEmpty(goodsList)) {
                log.info("没有需要更新结束时间和状态的拍卖商品");
                return;
            }
            for (AuctionGoodsPO auctionGoodsPO : goodsList) {
                LocalDateTime endTime = auctionGoodsService.getEndTime(String.valueOf(auctionGoodsPO.getId()), auctionGoodsPO.getEndTime());
                LocalDateTime nowTime = LocalDateTime.now();
                if (nowTime.isAfter(endTime)) {
                    updateEndStatus(auctionGoodsPO.getId(), endTime);
                    addOrder(auctionGoodsPO);
                }


            }
        }finally {
            log.info("结束进行拍卖结束任务....");
        }
    }

    /**
     * 添加订单
     * @param auctionGoodsPO
     */
    public void addOrder(AuctionGoodsPO auctionGoodsPO){
        AuctionOrderPO order = auctionOrderService.getOrderByGoodsId(auctionGoodsPO.getId());
        if( null == order ){
            return;
        }

        BidRedisVO latestPrice = auctionService.getLatestPrice(String.valueOf(auctionGoodsPO.getId()));
        if( null == latestPrice ){
            log.info("商品{}，id:{}没有最新出价记录，流拍",auctionGoodsPO.getGoodsName(),auctionGoodsPO.getId());
            return;
        }

        auctionOrderService.addOrder(latestPrice.getUuid(),auctionGoodsPO.getId(),latestPrice.getPrice());
    }

    /**
     * 更新结束状态
     * @param goodId
     * @param endTime
     */
    public void updateEndStatus(Long goodId,LocalDateTime endTime){
        LambdaUpdateWrapper<AuctionGoodsPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AuctionGoodsPO::getId, goodId);

        updateWrapper.set(AuctionGoodsPO::getSjEndTime,endTime);
        updateWrapper.set(AuctionGoodsPO::getStatus,GoodStatusEnum.JIESHU.getCode());

        auctionGoodsService.update(updateWrapper);
    }

    public List<AuctionGoodsPO> getGoodsList(){
        LambdaQueryWrapper<AuctionGoodsPO> query = new LambdaQueryWrapper<>();
        query.eq(AuctionGoodsPO::getStatus, GoodStatusEnum.WEIJIESHU.getCode());

        String nowTime = DateUtil.now();

        //开始时间小于当前时间且状态为0 则进行处理
        query.le(AuctionGoodsPO::getStartTime, nowTime);

        return auctionGoodsService.list(query);
    }
}
